package com.roger.springcloudGreenwich.service.impl;

import com.roger.springcloudGreenwich.bean.BlockInfoItem;
import com.roger.springcloudGreenwich.bean.Commodity;
import com.roger.springcloudGreenwich.repository.BlockInfoRepository;
import com.roger.springcloudGreenwich.service.BlockInfoService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2020/3/13.
 */
@Service
@Slf4j
public class BlockInfoServiceImpl implements BlockInfoService{
    @Autowired
    private BlockInfoRepository blockInfoRepository;
    @Override
    public void save(BlockInfoItem blockInfoItem) {
        blockInfoRepository.save(blockInfoItem);
    }

    @Override
    public Object findByName(String name) {
        List<BlockInfoItem> list = new ArrayList<>();

        log.info("name={}",name);

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchPhraseQuery("blockName", name))
                .build();

        Iterable<BlockInfoItem> iterable = blockInfoRepository.search(searchQuery);
        iterable.forEach(e->list.add(e));
        return list;
    }
}
